﻿using System;

namespace FallingRox
{
    public class KeyboardInput : IUserInput
    {
        public event EventHandler OnRightArrowPressed;

        public event EventHandler OnLeftArrowPressed;

        public event EventHandler OnUpArrowPressed;

        public event EventHandler OnDownArrowPressed;

        public event EventHandler OnSpaceBarPressed;

        public void HandleInput()
        {
            if (Console.KeyAvailable)
            {
                var keyPressed = Console.ReadKey();

                if (keyPressed.Key.Equals(ConsoleKey.LeftArrow))
                {
                    if (this.OnLeftArrowPressed != null)
                    {
                        this.OnLeftArrowPressed(this, new EventArgs());
                    }
                }

                if (keyPressed.Key.Equals(ConsoleKey.RightArrow))
                {
                    if (this.OnRightArrowPressed != null)
                    {
                        this.OnRightArrowPressed(this, new EventArgs());
                    }
                }

                if (keyPressed.Key.Equals(ConsoleKey.Spacebar))
                {
                    if (this.OnSpaceBarPressed != null)
                    {
                        this.OnSpaceBarPressed(this, new EventArgs());
                    }
                }

                if (keyPressed.Key.Equals(ConsoleKey.UpArrow))
                {
                    if (this.OnUpArrowPressed != null)
                    {
                        this.OnUpArrowPressed(this, new EventArgs());
                    }
                }

                if (keyPressed.Key.Equals(ConsoleKey.DownArrow))
                {
                    if (this.OnDownArrowPressed != null)
                    {
                        this.OnDownArrowPressed(this, new EventArgs());
                    }
                }
            }
        }
    }
}
